package com.alexsun.simplecalc

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import kotlin.math.sqrt

enum class OperatorType {
    PLUS, MINUS, TIMES, DIV
}

class MainActivity : AppCompatActivity(), View.OnClickListener {

    private var textBox: EditText? = null
    private var tempValue: Double? = null
    private var currOperator: OperatorType = OperatorType.PLUS

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        textBox = findViewById(R.id.edittext)

        for (btnID in arrayOf(
            R.id.button_ce,
            R.id.button_div,
            R.id.button_time,
            R.id.button_c,
            R.id.button_plus,
            R.id.button_minus,
            R.id.button_sqrt,
            R.id.button_eq,
        )) {
            findViewById<Button>(btnID).setOnClickListener(this)
        }
    }

    override fun onClick(v: View?) {
        val textBox = textBox!!
        when (v?.id) {
            R.id.button_c -> {
                tempValue = null
                textBox.setText("")
            }
            R.id.button_ce -> {
                textBox.setText("")
            }
            R.id.button_sqrt -> {
                val currValue = getNumber()
                if (currValue != null) {
                    val res = sqrt(currValue)
                    textBox.setText(res.toString().removeSuffix(".0"))
                }
            }
            R.id.button_plus -> {
                tempValue = getNumber()
                currOperator = OperatorType.PLUS
                textBox.setText("")
            }
            R.id.button_minus -> {
                tempValue = getNumber()
                currOperator = OperatorType.MINUS
                textBox.setText("")
            }
            R.id.button_time -> {
                tempValue = getNumber()
                currOperator = OperatorType.TIMES
                textBox.setText("")
            }
            R.id.button_div -> {
                tempValue = getNumber()
                currOperator = OperatorType.DIV
                textBox.setText("")
            }
            R.id.button_eq -> {
                if (tempValue == null) {
                    Toast.makeText(this, "请继续输入", Toast.LENGTH_SHORT).show()
                } else {
                    val currValue = getNumber() ?: return
                    val res = when (currOperator) {
                        OperatorType.PLUS -> {
                            tempValue!! + currValue
                        }
                        OperatorType.MINUS -> {
                            tempValue!! - currValue
                        }
                        OperatorType.TIMES -> {
                            tempValue!! * currValue
                        }
                        OperatorType.DIV -> {
                            tempValue!! / currValue
                        }
                    }
                    currValue.also { tempValue = it }
                    textBox.setText(res.toString().removeSuffix(".0"))
                }
            }
        }
    }

    private fun getNumber(): Double? {
        val s = textBox!!.text.toString()
        return try {
            s.toDouble()
        } catch (e: java.lang.NumberFormatException) {
            Toast.makeText(this, "输入不合法", Toast.LENGTH_SHORT).show()
            null
        }
    }

    fun inputNumber(v: View?) {
        if (v is Button) {
            val resText = textBox!!.text.toString() + v.text
            textBox!!.setText(resText)
        }
    }
}